package com.liuhongwei.test.builder;


import com.liuhongwei.test.bean.Constants;
import com.liuhongwei.test.bean.FieldInfo;
import com.liuhongwei.test.bean.TableInfo;
import com.liuhongwei.test.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @program: workspace-easyjava
 * @description:
 * @author: cw
 * @create: 2024-05-09 10:57
 **/
public class BuildService {

    private static final Logger log = LoggerFactory.getLogger(BuildService.class);


    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_SERVICE);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            String beanName = tableInfo.getBeanName();
            String beanParamName = tableInfo.getBeanParamName();
            String className = tableInfo.getBeanName() + "Service";

            os = new FileOutputStream(folder + "/" + className + ".java");
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.PACKAGE_SERVICE + ";\n\n");
            //写入Import
            writeImport(bw, beanName, beanParamName);
            //写入类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "Service");
            bw.write("public interface " + className + " {\n\n");

            //写入属性
            writeField(tableInfo, bw, beanName, beanParamName);

            bw.write("}");
            bw.flush();
        } catch (Exception e) {
            log.error("创建Service失败", e);
        } finally {
            BuildBase.close(os, osw, bw);
        }
    }


    private static void writeField(TableInfo tableInfo, BufferedWriter bw, String beanName, String beanParamName)
            throws Exception {
//        BuildComment.createFieldComment(bw, "根据条件查询列表");
//        bw.write("\tList<" + beanName + "> findListByParam(" + beanParamName + " query);\n\n");
//
//        BuildComment.createFieldComment(bw, "根据条件查询数量");
//        bw.write("\tInteger findCountByParam(" + beanParamName + " query);\n\n");
//
//        BuildComment.createFieldComment(bw, "分页查询");
//        bw.write("\tPaginationResultVO<" + beanName + "> findListByPage(" + beanParamName + " query);\n\n");
//
//        BuildComment.createFieldComment(bw, "新增");
//        bw.write("\tInteger add(" + beanName + " bean);\n\n");

        BuildComment.createFieldComment(bw, "页面分页查询");
        bw.write("\tJSONResponse getPage(JSONRequest jsonRequest) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "页面新增");
        bw.write("\tJSONResponse add(JSONRequest jsonRequest) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "页面更新");
        bw.write("\tJSONResponse update(JSONRequest jsonRequest) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "页面删除");
        bw.write("\tJSONResponse delete(JSONRequest jsonRequest) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "页面详情");
        bw.write("\tJSONResponse get(JSONRequest jsonRequest) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "新增批量");
        bw.write("\tInteger addBatch(List<" + beanName + "> listBean) throws BusinessException;\n\n");

        BuildComment.createFieldComment(bw, "新增批量或修改");
        bw.write("\tInteger addOrUpdateBatch(List<" + beanName + "> listBean) throws BusinessException;\n\n");

        writeFieldDynamic(tableInfo, bw, beanName);
    }

    private static void writeFieldDynamic(TableInfo tableInfo, BufferedWriter bw, String beanName) throws IOException {
        Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
        for (Entry<String, List<FieldInfo>> entry : keyIndexMap.entrySet()) {
            List<FieldInfo> fieldInfoList = entry.getValue();

            Integer index = 0;
            StringBuilder methodName = new StringBuilder();
            StringBuilder methodParam = new StringBuilder();
            for (FieldInfo fieldInfo : fieldInfoList) {
                index++;
                String propertyName = fieldInfo.getPropertyName();
                methodName.append(StringUtils.upperCaseFirst(propertyName));
                if (index < fieldInfoList.size()) {
                    methodName.append("And");
                }
                methodParam.append(fieldInfo.getJavaType() + " " + propertyName);
                if (index < fieldInfoList.size()) {
                    methodParam.append(", ");
                }
            }
            BuildComment.createFieldComment(bw, "根据" + methodName + "查询");
            bw.write("\t" + beanName + " selectBy" + methodName + "(" + methodParam + ") throws BusinessException;\n\n");

            BuildComment.createFieldComment(bw, "根据" + methodName + "更新");
            bw.write("\tInteger updateBy" + methodName + "(" + beanName + " bean, " + methodParam + ") throws BusinessException;\n\n");

            BuildComment.createFieldComment(bw, "根据" + methodName + "删除");
            bw.write("\tInteger deleteBy" + methodName + "(" + methodParam + ") throws BusinessException;\n\n");
        }
    }

    private static void writeImport(BufferedWriter bw, String beanName, String beanParamName) throws IOException {
        bw.write("import java.util.List;\n");
        bw.write("import com.alibaba.fastjson.JSONObject;\n");
        bw.write("import com.liuhongwei.test.entity.vo.JSONRequest;\n");
        bw.write("import com.liuhongwei.test.entity.vo.JSONResponse;\n");
        bw.write("import com.liuhongwei.test.exception.BusinessException;\n");
        bw.write("import " + Constants.PACKAGE_PO + "." + beanName + ";\n");

    }

    public static void main(String[] args) {
        List<TableInfo> tableInfoList = BuildTable.getTable();
        for (TableInfo tableInfo : tableInfoList) {
            BuildService.execute(tableInfo);
        }
    }

}
